<?php require_once('../connections/con_gl.php'); ?>
<?php
$nop = strtoupper($_POST['gudang']);
$act = $_GET[act];
if($act=='setcoa') {
	$url = "index.php?component=gudang&task=setcoa";	
}else{
	$url = "index.php?component=gudang";
}
$ope = $_POST['open'];
$data= $_POST['data'];
$jdata=count($data);
/* Validasi */
if($act <>'setcoa') { 
	if (trim($_POST['area']) == '') {
		$error[] = '- Area Operasional harus diisi';
	}
}
if (trim($_POST['gudang']) == '') {
	$error[] = '- Cabang harus diisi';
}
if($act <>'setcoa') { 
  if (trim($_POST['kodecabang']) == '') {
	$error[] = '- Kode Cabang harus diisi';
  }
}
if($act=='add' AND (trim($_POST['gudang']) <> '')) {
	// cek no polisi
	mysql_select_db($database_con_gl, $con_gl);
	$query_cekno = "select gudang from gl_gudang where gudang='$nop'";
	$cekno       = mysql_query($query_cekno, $con_gl);
	$row_cekno   = mysql_fetch_assoc($cekno);
	$total_cekno = mysql_num_rows($cekno);
	if($total_cekno > 0) { 
		$error[] = '- Nama cabang '.$nop.' sudah terdaftar';
	}
}

/*End validasi */
if (isset($error)) {
	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
}else{
	if($act =='add') {
	$query = "INSERT INTO gl_gudang (id, area, gudang, kodecabang, keterangan, tlp) VALUES (NULL, '$_POST[area]', '$_POST[gudang]', '$_POST[kodecabang]', '$_POST[keterangan]','$_POST[tlp]')";
	}else if($act=='edit'){
	$query ="UPDATE gl_gudang SET area='$_POST[area]', gudang='$_POST[gudang]', kodecabang='$_POST[kodecabang]', keterangan='$_POST[keterangan]', tlp='$_POST[tlp]' WHERE id='$_POST[id]'";
	}else if($act=='setcoa'){
	$query = "update gl_gudang set a_kas='$_POST[kasbank]', a_hut='$_POST[hutang]', a_piu='$_POST[piutang]', a_beb='$_POST[beban]', a_paj='$_POST[pajak]', a_hpp='$_POST[hpp]', a_pend='$_POST[pendapatan]' where gl_gudang.id='$_POST[gudang]'";
	if($jdata > 0) {
		$query_ = "update gl_akun set gudang='$_POST[gudang]' where ";
		for($is=0;$is<$jdata;$is++) {
			$query_ .= "id='$data[$is]'";
			if($is < $jdata-1) {
				$query_ .= " OR ";	
			}
		}
		mysql_query($query_, $con_gl) or die(mysql_error());
	}
	}else{
	$query = "delete from gl_gudang where id='$_POST[id]'";
	}
	mysql_select_db($database_con_gl, $con_gl);
	$runquery = mysql_query($query, $con_gl) or die(mysql_error());
	if($runquery) {
		echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil ";if($act=='add' OR $act=='edit' OR $act=='setcoa') { echo "disimpan";}else{ echo "dihapus"; } echo " ...";
	}else{
		echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
	}
}
?>
<?php if($runquery AND $ope=='window') { ?>
	<script type="text/javascript">top.opener.sendRespondCabang();self.close(2000);</script>
<?php }else if($runquery AND $ope==''){ ?>
	<script type="text/javascript">setTimeout("location.href='<?php echo $url;?>'", 2000);</script>
<?php } ?>